"use strict";
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activity/project/index' + location.search,
                    add_url: 'activity/project/add',
                    edit_url: 'activity/project/edit',
                    del_url: 'activity/project/del',
                    multi_url: 'activity/project/multi',
                    import_url: 'activity/project/import',
                    table: 'qsy_project_activity',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'shop.shopname', title: __('Shop.shopname'), operate: 'LIKE'},
                        {field: 'type.label', title: __('Type.label'), operate: 'LIKE'},
                        {field: 'title', title: __('Title')},
                        {field: 'cover_img', title: __('Cover_img'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'label', title: __('Label'), operate: 'LIKE'},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4')}, formatter: Table.api.formatter.status},
                        // {field: 'day_num', title: __('Day_num'), operate: false},
                        {field: 'sold_num', title: __('Sold_num'), operate: false},
                        // {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'audit_time', title: __('Audit_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {
                            field: "operate",
                            title: __("Operate"),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                name: "agree",
                                title: __("活动通过审核"),
                                classname: "btn btn-xs btn-success btn-magic btn-ajax",
                                icon: "fa fa-check",
                                text: "通过",
                                confirm: "确认点击通过，通过活动审核？",
                                url: "activity/project/agree",
                                visible: function (e) {
                                    if (0 == e.status) return !0
                                },
                                success: function (e, a) {
                                    return table.bootstrapTable("refresh"), !1
                                },
                                error: function (e, t) {
                                    return console.log(e, t), Layer.alert(t.msg), !1
                                }
                            }, {
                                name: "refuse",
                                title: __("活动审核不通过"),
                                classname: "btn btn-xs btn-danger btn-dialog",
                                icon: "fa fa-times",
                                text: "不通过",
                                url: "activity/project/refuse",
                                visible: function (e) {
                                    if (0 == e.status) return !0
                                },
                                extend: 'data-area=["500px","270px"]'
                            }, {
                                name: "detail",
                                title: __("详情"),
                                classname: "btn btn-xs btn-info btn-dialog",
                                icon: "fa fa-eye",
                                url: "activity/project/detail"
                            }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        refuse: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
